package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-07-12
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    private EduCourseService eduCourseService;
    @Autowired
    public void setEduCourseService(EduCourseService eduCourseService) {
        this.eduCourseService = eduCourseService;
    }

    // 获取课程列表
    @GetMapping("/getCourseList")
    public R getCourseList(){
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list",list);
    }

    // 课程列表的分页查询
    @GetMapping("/pageCourse/{current}/{limit}")
    public R pageListCourse(@PathVariable long current,@PathVariable long limit){
        //填入当前页数与当前页所含项目数
        Page<EduCourse> pageCourse = new Page<>(current,limit);
        //调用教师service把数据封装进pageTeacher中
        eduCourseService.page(pageCourse,null);
        //取出数据
        long total = pageCourse.getTotal();
        List<EduCourse> courses = pageCourse.getRecords();
        //封装进返回json信息
        return R.ok().data("total",total).data("rows",courses);
    }

    // 添加课程信息
    @PostMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        System.out.println(courseInfoVo);
        String id = eduCourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    // 根据id来删除课程
    @DeleteMapping("/{courseId}")
    public R deleteCourseById(@PathVariable String courseId){
        // 要删除课程及其以下的所有课程与小节还有课程介绍
        if(eduCourseService.removeCourse(courseId)){
            return R.ok();
        }
        return R.error();
    }

    // 根据课程id获取课程信息
    @GetMapping("/getCourseInfo/{courseId}")
    public R getCourseInfoByCourseId(@PathVariable("courseId") String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    // 根据课程id获取课程信息(被人远程调用的接口)
    @GetMapping("/getDto/{courseId}")
    public CourseInfoVo getCourseInfoDto(@PathVariable("courseId") String courseId){
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(courseId);
        return courseInfoVo;
    }

    @PostMapping("/pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable long current,@PathVariable long limit, @RequestBody CourseQuery courseQuery){
        Page<EduCourse> pageCourse = new Page<>(current,limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        // 取出数据
        String status = courseQuery.getStatus();
        String likeName = courseQuery.getName();
        // 构建条件
        if(!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        if(!StringUtils.isEmpty(likeName)){
            wrapper.like("title", likeName);
        }
        // 排序
        wrapper.orderByDesc("gmt_create");
        eduCourseService.page(pageCourse,wrapper);
        //取出数据
        long total = pageCourse.getTotal();
        List<EduCourse> courses = pageCourse.getRecords();
        return R.ok().data("total",total).data("rows",courses);
    }

    // 根据课程id来更新课程信息
    @PostMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        eduCourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    // 根据课程id来查询确认课程信息
    @GetMapping("/getPublishCourseInfo/{courseId}")
    public R getPublishCourseInfo(@PathVariable String courseId){
        CoursePublishVo coursePublishVo = eduCourseService.getPublishCourseInfo(courseId);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    // 修改课程为发布状态
    @PostMapping("/publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(courseId);
        eduCourse.setStatus("Normal");
        eduCourseService.updateById(eduCourse);
        return R.ok();
    }
}

